package com.ruoyi.restaurant.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.restaurant.domain.TestChild;
import com.ruoyi.restaurant.mapper.TestMainMapper;
import com.ruoyi.restaurant.domain.TestMain;
import com.ruoyi.restaurant.service.ITestMainService;

/**
 * 测试主1Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-18
 */
@Service
public class TestMainServiceImpl implements ITestMainService 
{
    @Autowired
    private TestMainMapper testMainMapper;

    /**
     * 查询测试主1
     * 
     * @param firstId 测试主1主键
     * @return 测试主1
     */
    @Override
    public TestMain selectTestMainByFirstId(Integer firstId)
    {
        return testMainMapper.selectTestMainByFirstId(firstId);
    }

    /**
     * 查询测试主1列表
     * 
     * @param testMain 测试主1
     * @return 测试主1
     */
    @Override
    public List<TestMain> selectTestMainList(TestMain testMain)
    {
        return testMainMapper.selectTestMainList(testMain);
    }

    /**
     * 新增测试主1
     * 
     * @param testMain 测试主1
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestMain(TestMain testMain)
    {
        int rows = testMainMapper.insertTestMain(testMain);
        insertTestChild(testMain);
        return rows;
    }

    /**
     * 修改测试主1
     * 
     * @param testMain 测试主1
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestMain(TestMain testMain)
    {
        testMainMapper.deleteTestChildByFirstId(testMain.getFirstId());
        insertTestChild(testMain);
        return testMainMapper.updateTestMain(testMain);
    }

    /**
     * 批量删除测试主1
     * 
     * @param firstIds 需要删除的测试主1主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestMainByFirstIds(Integer[] firstIds)
    {
        testMainMapper.deleteTestChildByFirstIds(firstIds);
        return testMainMapper.deleteTestMainByFirstIds(firstIds);
    }

    /**
     * 删除测试主1信息
     * 
     * @param firstId 测试主1主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestMainByFirstId(Integer firstId)
    {
        testMainMapper.deleteTestChildByFirstId(firstId);
        return testMainMapper.deleteTestMainByFirstId(firstId);
    }

    /**
     * 新增测试子1信息
     * 
     * @param testMain 测试主1对象
     */
    public void insertTestChild(TestMain testMain)
    {
        List<TestChild> testChildList = testMain.getTestChildList();
        Integer firstId = testMain.getFirstId();
        if (StringUtils.isNotNull(testChildList))
        {
            List<TestChild> list = new ArrayList<TestChild>();
            for (TestChild testChild : testChildList)
            {
                testChild.setFirstId(firstId);
                list.add(testChild);
            }
            if (list.size() > 0)
            {
                testMainMapper.batchTestChild(list);
            }
        }
    }


    @Override
    public List<Map<String, Object>> selectListCount(TestMain testMain) {

        return testMainMapper.selectListCount(testMain);
    }
}
